window._MWORK = {

    _MAINLABEL:null,

    showWork:function(parentNode,param){

        var uiObj = _GRM.getNodeObj('renwuLabel');
        var mainLabel = uiObj.node;
        _MWORK._MAINLABEL = mainLabel;

        if(uiObj.isnew){
            _MWORK.initButton(mainLabel);
        }

        _MWORK.refresh(true);
        parentNode.addChild(mainLabel);
        return mainLabel;

    },


    refresh:function(toTop){
        var mainLabel = _MWORK._MAINLABEL;
        var scrollviewNode = mainLabel.getChildByName('scrollview');
        var workNodes = scrollviewNode
                    .getChildByName('view')
                    .getChildByName('content').getChildren();

        if(toTop){
            scrollviewNode.getComponent(cc.ScrollView).setContentPosition(cc.v2(0,0));
        }

        for(var i=0;i<workNodes.length;i++){
            _MWORK.refreshOne(workNodes[i],window.gameData.workList[i]);
        }

    },

    refreshOne:function(workNode,workInfo){
        if(workInfo){
            workNode.active = true;
            var npcConfig = _NPC[workInfo.npc];
            var faceNode = workNode.getChildByName('touxian');
            var nameNode = faceNode.getChildByName('name');
            var messageNode = workNode.getChildByName('content');
            var levelNode = workNode.getChildByName('level');
            var darkNode = workNode.getChildByName('biaozi').getChildByName('img_e');
            var MainNode = workNode.getChildByName('biaozi').getChildByName('img_san');
            var fightNode = workNode.getChildByName('biaozi').getChildByName('img_gong');
            var btnCancel = workNode.getChildByName('buttonCancel');

            if(npcConfig){
                faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(npcConfig.face);
            }else{
                faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame('问号');
            }
            nameNode.getComponent(cc.Label).string = workInfo.npc;
            messageNode.getComponent(cc.Label).string = _MWORK.getWorkInfoContent(workInfo);
            levelNode.getComponent(cc.Label).string ="等级:"+workInfo.level;

            if(workInfo.isDark){
                darkNode.active = true;
            }else{
                darkNode.active = false;
            }

            if(workInfo.isHonor){
                fightNode.active = true;
            }else{
                fightNode.active = false;
            }   

            if(workInfo.isMain){
                MainNode.active = true;
                btnCancel.active = false;
            }else{
                MainNode.active = false;
                btnCancel.active = true;
            }

        }else{
            workNode.active = false;
        }
    },


    initButton:function(mainLabel){

        var workNodes = mainLabel.getChildByName('scrollview')
                                .getChildByName('view')
                                .getChildByName('content').getChildren();
     
        mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, mainLabel);

        for(var i=0;i<workNodes.length;i++){

            var btnCancel = workNodes[i].getChildByName('buttonCancel');
            var btnComplete = workNodes[i].getChildByName('buttonComplete');
            var btnChange = workNodes[i].getChildByName('buttongenhuan');

            btnComplete.workIndex = i;
            btnCancel.workIndex = i;
            btnChange.workIndex =i;

            btnChange.on(cc.Node.EventType.TOUCH_START, function(){
                _MWORK.changeWork(this.workIndex);
            }, btnChange);

            btnCancel.on(cc.Node.EventType.TOUCH_START, function(){
                _MWORK.cancelWork(this.workIndex);
            }, btnCancel);

            btnComplete.on(cc.Node.EventType.TOUCH_START, function(){
                _MWORK.completeWork(this.workIndex);
            }, btnComplete);
        }

    },

    changeWork:function(workIndex){
        if(window.gameData.workList[workIndex]){

            //if(_MWORK.isCompletedWork(window.gameData.workList[workIndex])){
            //    _TIPS.showMessage('此任务已经完成无法更换');
            //    return;
            //}

            if(window.gameData.workList[workIndex].isMain){
                _TIPS.showMessage('主线任务无法更换');
                return;
            }

            if(window.gameData.workList[workIndex].type=="fight"
                &&_MWORK.isCompletedWork(window.gameData.workList[workIndex])){
                _TIPS.showMessage('此任务已经完成无法放弃');
                //_CHOICEMANAGER.showChoice(_MAP.node,'giveUpWork',{workIndex:workIndex});
                return;
            }

            var npc = window.gameData.workList[workIndex].npc;
            var oldtype = window.gameData.workList[workIndex].type;
            var newwork = _GAMEDATAHELPER.randomWork(npc,oldtype);
            window.gameData.workList[workIndex]=newwork;
            _MWORK.refresh(false);
        }
    },

    resetWork:function(workIndex){
        if(window.gameData.workList[workIndex]){

            if(window.gameData.workList[workIndex].isMain){
                var newtask = _MWORK.getMainTaskByIndex(window.gameData.workList[workIndex].index+1);
                if(newtask){
                    window.gameData.workList[workIndex]=newtask;
                }else{
                    window.gameData.workList.splice(workIndex,1);
                }
            }else{
                var workInfo = window.gameData.workList[workIndex];
                if(workInfo.type=="fight"){
                    for(var i=0;i<workInfo.target.length;i++){
                        workInfo.target[i].current = workInfo.target[i].current-workInfo.target[i].nmb;
                    }
                }
            }
            _MWORK.refresh(false);
        }
    },

    cancelWork:function(workIndex){
        if(window.gameData.workList[workIndex]){

            
            if(window.gameData.workList[workIndex].isMain){
                _TIPS.showMessage('主线任务无法放弃');
                //_CHOICEMANAGER.showChoice(_MAP.node,'giveUpMainWork',{workIndex:workIndex});
                return;
            }

            if(window.gameData.workList[workIndex].type=="fight"
                &&_MWORK.isCompletedWork(window.gameData.workList[workIndex])){
                _TIPS.showMessage('此任务已经完成无法放弃');
                //_CHOICEMANAGER.showChoice(_MAP.node,'giveUpWork',{workIndex:workIndex});
                return;
            }

            window.gameData.workList.splice(workIndex,1);
            _MWORK.refresh(false);
        }
    },

    completeWork:function(workIndex){
        var workInfo = window.gameData.workList[workIndex];
        if(workInfo){
            
            if(_MWORK.isCompletedWork(workInfo)){

                _MWORK.getReward(workInfo);
                _MWORK.resetWork(workIndex);
                _MWORK.refresh(false);

            }else{

                _TIPS.showMessage('该任务尚不满足完成条件！(锁住的道具无法提交任务)',0);

            }

        }
    },

    getReward:function(workInfo){

        if(workInfo&&workInfo.reward){

            var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.npc);
            if(npclog){
                _GAMEDATAHELPER.addNpcF(workInfo.npc,20);
            }

            if(workInfo.isHonor){
                _GAMEDATAHELPER.addLevelData('levelFight',5);
            }

            if(workInfo.isDark){
                _GAMEDATAHELPER.addLevelData('levelDark',1);
            }

            if(_GAMEDATAHELPER.hasTeamSkill('yangming')){
                _GAMEDATAHELPER.addLevelData('levelBright',1);
            }

            var message = "任务完成,获得:";

            if(workInfo.index||workInfo.index==0){
                _TTCH.doCompleteTask('completeTask',workInfo.index,true);
            }

            //console.log('完成任务:',workInfo.reward);

            for(var i=0;i<workInfo.reward.length;i++){

                var rewardObj = workInfo.reward[i];

                if(rewardObj.type=="gold"){
                    message+="\n钱*"+rewardObj.nmb;
                    _GAMEDATAHELPER.oprMoney(rewardObj.nmb);
                }else if(rewardObj.type=="exp"){
                    message+="\n经验*"+rewardObj.nmb;
                    var maxlv = _PLAYERSTATUS.getLevelMax(null);
                    for(var j=0;j<window.gameData.playerTeam.length;j++){
                        if(window.gameData.playerTeam[j].hp>0
                        &&!window.gameData.playerTeam[j].isCustomRole){
                            _GAMEDATAHELPER.addExp(window.gameData.playerTeam[j],rewardObj.nmb,maxlv);
                        }
                    }
                }else if(rewardObj.type=="openChapter"){

                    message+="\n解锁:"+rewardObj.value;
                    _GAMEDATAHELPER.openChapter(rewardObj.value);

                }else if(rewardObj.type=="getNpc"){
                    message+="\n角色:"+rewardObj.value;
                    var tmpnpc = _CROF.getRandomMainRole(false,rewardObj.value,null);
                    window.gameData.playerTeamBack.unshift(tmpnpc);
                }else{
                    for(var j=0;j<rewardObj.nmb;j++){
                        _GAMEDATAHELPER.pushItem(_IOF.createObjByName(rewardObj.type,null));
                    }
                    message+="\n"+rewardObj.type+"*"+rewardObj.nmb;
                }

            }

            _TIPS.showMessage(message,0);

        }

    },

    isCompletedWork:function(workInfo){

        var completeFlag = true;
        if(workInfo.type=="fight"){
            for(var i=0;i<workInfo.target.length;i++){
                if(workInfo.target[i].nmb>workInfo.target[i].current){
                    completeFlag = false;
                }
            }
        }else if(workInfo.type=="item"){
            for(var i=0;i<workInfo.target.length;i++){
                var current = _GAMEDATAHELPER.getGoodsNmb(window.gameData[workInfo.arr],workInfo.target[i].type);
                if(current<workInfo.target[i].nmb){
                    completeFlag = false;
                }
            }

            if(completeFlag){
                for(var i=0;i<workInfo.target.length;i++){
                    var arr = window.gameData[workInfo.arr];
                    var index = _GAMEDATAHELPER.getUnlockGoodsIndex(arr,workInfo.target[i].type);
                    if(index!=-1){
                        var nmb = workInfo.target[i].nmb;
                        _GAMEDATAHELPER.reduceGoods(arr,index,nmb);
                    }else{
                        completeFlag = false;
                    }
                }
            }
        }else if(workInfo.type=="meet"){
            for(var i=0;i<workInfo.target.length;i++){
                var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.target[i]);
                if(!npclog||!npclog.sign){
                    completeFlag=false;
                }
            }
        }else if(workInfo.type=="work"){
            for(var i=0;i<workInfo.target.length;i++){
                if(workInfo.target[i].current<workInfo.target[i].nmb){
                    completeFlag=false;
                }
            }
        }else if(workInfo.type=="friend"){
            for(var i=0;i<workInfo.target.length;i++){
                var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.target[i].npc);
                var current = npclog.f;
                if(current<workInfo.target[i].nmb){
                    completeFlag=false;
                }
            }
        }else if(workInfo.type=="getnpc"){
            for(var i=0;i<workInfo.target.length;i++){
                var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.target[i]);
                if(!npclog.zm){
                    completeFlag=false;
                }
            }
        }else if(workInfo.type=="getMap"){
            for(var i=0;i<workInfo.target.length;i++){
                if(!window.gameData.mapSignLog[workInfo.target[i]])
                    completeFlag=false;
            }
        }else if(workInfo.type=="teamSkill"){
            for(var i=0;i<workInfo.target.length;i++){
                if(!window.gameData.cjLog[workInfo.target[i]]){
                    completeFlag=false;
                }
            }
        }else if(workInfo.type=="growUp"){
            var playerLv = _EXPMANAGER.getLevelByExp(window.gameData.playerTeam[0].exp);
            if(playerLv<workInfo.target){
                completeFlag=false;
            }
        }else if(workInfo.type=="story"){
            if(!_GAMEDATAHELPER.isLogStory(workInfo.target))
                completeFlag=false;
        }else if(workInfo.type=="passChapter"){
            completeFlag=workInfo.target.isPass;
        }

        return completeFlag;
    },

    getWorkInfoContent:function(workInfo){

        var message = "";

        if(workInfo.target){
           
            if(workInfo.type=="fight"){
                message+="野外击杀下列目标:";
                for(var i=0;i<workInfo.target.length;i++){
                    message+="\n"+workInfo.target[i].type+"(lv."+workInfo.target[i].lv+"):"+workInfo.target[i].current+"/"+workInfo.target[i].nmb;
                }
            }else if(workInfo.type=="item"){
                message+="上交下列道具:";
                for(var i=0;i<workInfo.target.length;i++){
                    var current = _GAMEDATAHELPER.getGoodsNmb(window.gameData[workInfo.arr],workInfo.target[i].type);
                    message+="\n"+workInfo.target[i].type+":"+current+"/"+workInfo.target[i].nmb;
                }
            }else if(workInfo.type=="meet"){
                message+="拜访下列NPC:";
                for(var i=0;i<workInfo.target.length;i++){
                    var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.target[i]);
                    var current = npclog.sign?1:0;
                    message+="\n"+workInfo.target[i]+":"+current+"/1";
                }
            }else if(workInfo.type=="work"){
                message+="完成下列NPC任务:";
                for(var i=0;i<workInfo.target.length;i++){
                    message+="\n"+workInfo.target[i].npc+":"+workInfo.target[i].current+"/"+workInfo.target[i].nmb;
                }
            }else if(workInfo.type=="friend"){
                message+="提升下列NPC友好:";
                for(var i=0;i<workInfo.target.length;i++){
                    var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.target[i].npc);
                    var current = npclog.f;
                    message+="\n"+workInfo.target[i].npc+":"+current+"/"+workInfo.target[i].nmb;
                }
            }else if(workInfo.type=="getnpc"){
                message+="招募下列NPC:";
                for(var i=0;i<workInfo.target.length;i++){
                    var npclog = _GAMEDATAHELPER.getNpcLog(workInfo.target[i]);
                    message+="\n"+workInfo.target[i]+":"+npclog.zm+"/1";
                }
            }else if(workInfo.type=="getMap"){
                message+="解锁下列城市传送:";
                for(var i=0;i<workInfo.target.length;i++){
                    var isopen = window.gameData.mapSignLog[workInfo.target[i]]?1:0;
                    message+="\n"+workInfo.target[i]+":"+isopen+"/1";
                }
            }else if(workInfo.type=="teamSkill"){
                message+="解锁下列异能:";
                for(var i=0;i<workInfo.target.length;i++){
                    var isopen = window.gameData.cjLog[workInfo.target[i]]?1:0;
                    message+="\n"+workInfo.target[i]+":"+isopen+"/1";
                }
            }else if(workInfo.type=="growUp"){
                message+="等级到达:";
                var playerLv = _EXPMANAGER.getLevelByExp(window.gameData.playerTeam[0].exp);
                message+=playerLv+"/"+workInfo.target;
            }else if(workInfo.type=="story"){
                message+="完成剧情:"+workInfo.target+":"+(_GAMEDATAHELPER.isLogStory(workInfo.target)?1:0)+"/1";
            }else if(workInfo.type=="passChapter"){
                message+="通关‘"+workInfo.target.chapter+"’第"+workInfo.target.chapterIndex+"层("+(workInfo.target.isPass?1:0)+"/1)";
            }
        }

        if(workInfo.reward){
            message+="\n奖励:";
            for(var i=0;i<workInfo.reward.length;i++){
                if(workInfo.reward[i].type=="gold"){
                    message+="\n钱:"+workInfo.reward[i].nmb;
                }else if(workInfo.reward[i].type=="exp"){
                    message+="\n经验:"+workInfo.reward[i].nmb;
                }else if(workInfo.reward[i].type=="openChapter"){
                    message+="\n解锁:"+workInfo.reward[i].value;
                }else{
                    message+="\n"+workInfo.reward[i].type+":"+workInfo.reward[i].nmb;
                }
            }
        }

        if(workInfo.tips){
            message+="\n提示:"+workInfo.tips;
        }
        
        return message;
    },

    getMainTaskByIndex:function(index){

        if(_MAINTASK[index]){

            var cloneTask = _CU.cloneObj(_MAINTASK[index]);
            cloneTask.isMain = 1;
            if(cloneTask.reward.length==0){
                if(_TASKREWARD[cloneTask.level]){
                    cloneTask.reward =_CU.cloneObj(_TASKREWARD[cloneTask.level]);
                }

                var nextTask = _MAINTASK[index+1];
                if(nextTask
                    &&nextTask.target
                    &&nextTask.target.chapter){

                    cloneTask.reward.push({type:"openChapter",value:nextTask.target.chapter});

                }
                //var randomItem = _FIGHTITEMHELPER.randomItemByLevel(cloneTask.level/1);
                //if(randomItem){
                //    cloneTask.reward.push({type:randomItem,nmb:5});
                //}
            }
            cloneTask.index = index;
            cloneTask.npc = '???';
            return cloneTask;
        }

    },

    

}